<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="巡区名称" prop="areaName">
        <el-input
          v-model="queryParams.areaName"
          placeholder="请输入巡区名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分配类型" prop="assignType">
        <el-select
          v-model="queryParams.assignType"
          placeholder="分配类型"
          clearable
          style="width: 240px"
        >
          <el-option label="主要负责人" value="1" />
          <el-option label="协助人员" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增分配</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="assignList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="巡区名称" align="center" prop="areaName" />
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="分配类型" align="center" prop="assignType">
        <template slot-scope="scope">
          <el-tag :type="scope.row.assignType === 1 ? 'primary' : 'success'">
            {{ scope.row.assignType === 1 ? '主要负责人' : '协助人员' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="分配时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增/修改分配对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="巡区" prop="areaId">
              <el-select
                v-model="form.areaId"
                placeholder="请选择巡区"
                style="width: 100%"
                filterable
              >
                <el-option
                  v-for="area in areaOptions"
                  :key="area.areaId"
                  :label="area.areaName"
                  :value="area.areaId"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="用户" prop="userId">
              <el-select
                v-model="form.userId"
                placeholder="请选择用户"
                style="width: 100%"
                filterable
              >
                <el-option
                  v-for="user in userOptions"
                  :key="user.userId"
                  :label="user.nickName + ' (' + user.userName + ')'"
                  :value="user.userId"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="分配类型" prop="assignType">
              <el-radio-group v-model="form.assignType">
                <el-radio :label="1">主要负责人</el-radio>
                <el-radio :label="2">协助人员</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPatrolAreaUser, getPatrolAreaUser, addPatrolAreaUser, updatePatrolAreaUser, delPatrolAreaUser } from '@/api/system/patrolAreaUser';
import { listPatrolArea } from '@/api/system/patrolArea';
import { listUser } from '@/api/system/user';
import Pagination from "@/components/Pagination";

export default {
  name: 'PatrolAreaAssign',
  components: { Pagination },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaName: undefined,
        userName: undefined,
        assignType: undefined
      },
      // 表单参数
      form: {
        id: undefined,
        areaId: undefined,
        userId: undefined,
        assignType: 1,
        remark: undefined
      },
      // 表单校验
      rules: {
        areaId: [
          { required: true, message: '请选择巡区', trigger: 'change' }
        ],
        userId: [
          { required: true, message: '请选择用户', trigger: 'change' }
        ],
        assignType: [
          { required: true, message: '请选择分配类型', trigger: 'change' }
        ]
      },
      // 分配列表数据
      assignList: [],
      // 巡区选项
      areaOptions: [],
      // 用户选项
      userOptions: []
    };
  },
  created() {
    this.getList();
    this.getAreaOptions();
    this.getUserOptions();
  },
  methods: {
    /** 查询分配列表 */
    getList() {
      this.loading = true;
      listPatrolAreaUser(this.queryParams).then(response => {
        this.assignList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 获取巡区选项 */
    getAreaOptions() {
      listPatrolArea({ pageSize: 1000 }).then(response => {
        this.areaOptions = response.rows || [];
      });
    },
    /** 获取用户选项 */
    getUserOptions() {
      listUser({ pageSize: 1000, status: '0' }).then(response => {
        this.userOptions = response.rows || [];
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加分配';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getPatrolAreaUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改分配';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id) {
            updatePatrolAreaUser(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addPatrolAreaUser(this.form).then(response => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除分配编号为"' + ids + '"的数据项?').then(() => {
        return delPatrolAreaUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess('删除成功');
      }).catch(() => {});
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        areaId: undefined,
        userId: undefined,
        assignType: 1,
        remark: undefined
      };
      this.resetForm("form");
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  .mb8 {
    margin-bottom: 8px;
  }
}
</style> 